package com.andy.blog.admin.controller;

import com.andy.blog.admin.dto.SysConfigDTO;
import com.andy.blog.admin.service.ConfigBusService;
import com.andy.blog.common.annotation.Log;
import com.andy.blog.common.enums.BusinessTypeEnum;
import com.andy.blog.common.page.PageUtilsDTO;
import com.andy.blog.common.reponse.GeneralResponseResult;
import com.andy.blog.common.utils.EasyExcelUtils;
import com.andy.blog.dal.vo.SysConfigVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 配置中心
 * Created by andy on 2023-04-05 11:38
 */
@RestController
@RequestMapping("/system/config")
public class SysConfigController {

    @Resource
    private ConfigBusService configBusService;

    /**
     * 获取参数配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:config:list')")
    @GetMapping("/list")
    public GeneralResponseResult<PageUtilsDTO<SysConfigDTO>> list(SysConfigVO sysConfigVO) {
        return GeneralResponseResult.success(configBusService.selectListByPage(sysConfigVO));
    }

    /**
     * 导出
     */
    @Log(title = "配置中心", businessType = BusinessTypeEnum.EXPORT)
    @PreAuthorize("@ss.hasPermi('system:config:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysConfigVO sysConfigVO) {
        List<SysConfigDTO> list = configBusService.selectListByCondition(sysConfigVO);
        EasyExcelUtils.writeStyleExcel(response, list, "参数数据", SysConfigDTO.class);
    }

    /**
     * 根据参数编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:config:query')")
    @GetMapping(value = "/{id}")
    public GeneralResponseResult<SysConfigDTO> getInfo(@PathVariable Long id) {
        return GeneralResponseResult.success(configBusService.selectById(id));
    }

    /**
     * 根据参数键名查询参数值
     */
    @GetMapping(value = "/configKey/{configKey}")
    public GeneralResponseResult<String> getConfigKey(@PathVariable String configKey) {
        return GeneralResponseResult.success(configBusService.selectConfigByKey(configKey));
    }

    /**
     * 新增参数配置
     */
    @PreAuthorize("@ss.hasPermi('system:config:add')")
    @Log(title = "配置中心", businessType = BusinessTypeEnum.INSERT)
    @PostMapping
    public GeneralResponseResult<String> add(@Validated @RequestBody SysConfigVO configVO) {
        configBusService.insertConfig(configVO);
        return GeneralResponseResult.success();
    }

    /**
     * 修改参数配置
     */
    @PreAuthorize("@ss.hasPermi('system:config:edit')")
    @Log(title = "配置中心", businessType = BusinessTypeEnum.UPDATE)
    @PutMapping
    public GeneralResponseResult<String> edit(@Validated @RequestBody SysConfigVO configVO) {
        configBusService.updateConfig(configVO);
        return GeneralResponseResult.success();
    }

    /**
     * 删除参数配置
     */
    @PreAuthorize("@ss.hasPermi('system:config:remove')")
    @Log(title = "配置中心", businessType = BusinessTypeEnum.DELETE)
    @DeleteMapping("/{configId}")
    public GeneralResponseResult<Void> remove(@PathVariable Long configId) {
        configBusService.deleteById(configId);
        return GeneralResponseResult.success();
    }

    /**
     * 参数配置-刷新缓存
     */
    @PreAuthorize("@ss.hasPermi('system:config:remove')")
    @Log(title = "配置中心", businessType = BusinessTypeEnum.CLEAN)
    @DeleteMapping("/refreshCache")
    public GeneralResponseResult<Void> refreshCache() {
        configBusService.resetConfigCache();
        return GeneralResponseResult.success();
    }
}
